package com.cade.agriculturemall.view.home.search;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cade.agriculturemall.R;
import com.cade.agriculturemall.model.SearchRecord;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hshunxin on 2018/2/10.
 */

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> {
    private SearchActivity mContext;
    private List<SearchRecord> localList = new ArrayList<>();
    private List<SearchRecord> recommendList = new ArrayList<>();
    private LayoutInflater mInflater;
    private int SELECTION_LABEL = 1;
    private int RECOMMEND_LABEL = 2;
    private int SELECTION_TEXT = 3;
    private int RECOMMEND_TEXT = 4;
    private int CUSTOM_LABEL = 5;
    private boolean localEmpty = false;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == SELECTION_LABEL) {//选择标签
            return new ViewHolder(mInflater.inflate(R.layout.mc_patient_label_adapter_item, null));
        } else if (viewType == SELECTION_TEXT) {//选择文本
            return new ViewHolder(mInflater.inflate(R.layout.mc_selected_label_adapter_item2, null));
        } else if (viewType == RECOMMEND_TEXT) {//推荐文本
            return new ViewHolder(mInflater.inflate(R.layout.mc_selected_label_adapter_item3, null));
        } else if (viewType == CUSTOM_LABEL) {//自定义标签
            return new ViewHolder(mInflater.inflate(R.layout.mc_patient_label_adapter_item, null));
        } else {//推荐标签
            return new ViewHolder(mInflater.inflate(R.layout.mc_patient_label_adapter_item, null));
        }
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final int viewType = getItemViewType(position);
        if (viewType == SELECTION_LABEL) {//选择标签
            holder.mTextView.setText(localList.get(position - 1).getVaule());
        } else if (viewType == SELECTION_TEXT) {//选择文本
            holder.mTextView.setText("最近搜索");
            holder.mImageDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(mContext, "清除", Toast.LENGTH_SHORT).show();
                    localList.clear();
                    localEmpty = true;
                    mContext.deleteData();
                }
            });
        } else if (viewType == RECOMMEND_TEXT) {//推荐文本
            holder.mTextView.setText("热门搜索");
        } else if (viewType == RECOMMEND_LABEL) {//自定义标签
            if (localEmpty) {
                holder.mTextView.setText(recommendList.get(position - 1).getVaule());
            } else {
                holder.mTextView.setText(recommendList.get(position - localList.size() - 2).getVaule());
            }
        }

        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (viewType == SELECTION_LABEL) {
                    Toast.makeText(mContext, "本地查询", Toast.LENGTH_SHORT).show();
                    mContext.itemSearch(localList.get(position).getVaule());
                }
                if (viewType == RECOMMEND_LABEL) {
                    Toast.makeText(mContext, "推荐查询", Toast.LENGTH_SHORT).show();
                    if (localEmpty) {
                        mContext.itemSearch(recommendList.get(position - 1).getVaule());
                    } else {
                        mContext.itemSearch(recommendList.get(position - localList.size() - 2).getVaule());
                    }
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if (localEmpty) {
            if (position == 0) {
                return RECOMMEND_TEXT;
            } else {
                return RECOMMEND_LABEL;
            }
        } else {
            if (position == 0) {
                return SELECTION_TEXT;
            } else if (position <= localList.size()) {
                return SELECTION_LABEL;
            } else if (position == localList.size() + 1) {
                return RECOMMEND_TEXT;
            } else {
                return RECOMMEND_LABEL;
            }
        }
    }

    public SearchAdapter(SearchActivity context, List<SearchRecord> listLocal, List<SearchRecord> listRecommend) {
        if (listLocal.size() == 0)
            localEmpty = true;
        localList = listLocal;
        mContext = context;
        mInflater = LayoutInflater.from(context);
        if (listRecommend != null) {
//            listRecommend = listRecommend;
        }
        recommendList.add(new SearchRecord("1", "a"));
        recommendList.add(new SearchRecord("2", "b"));
        recommendList.add(new SearchRecord("3", "c"));
        recommendList.add(new SearchRecord("4", "d"));
    }


    @Override
    public int getItemCount() {
        if (localEmpty) {
            return recommendList.size() + 1;
        } else {
            return localList.size() + recommendList.size() + 2;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView mTextView;
        private ImageView mImageDelete;

        public ViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.p_patientLabel_adapter_bt);
            mImageDelete = (ImageView) itemView.findViewById(R.id.p_patientLabel_adapter_img);
        }
    }
}
